package com.neutech.web;

import com.neutech.dao.UserDAO;
import com.neutech.dao.impl.UserDAOImpl;
import com.neutech.entity.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "userServlet",urlPatterns = {"*.user"})
public class UserServlet extends HttpServlet {

    private UserDAO dao = new UserDAOImpl();
    public UserServlet(){
        System.out.println("对象已经实例化。。。");
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 进行前端用户请求的拦截判断
        // 根据用户请求地址栏(url)不同，进行不同的业务逻辑操作
        String path = request.getRequestURL().toString();
        path = path.substring(path.lastIndexOf("/"),path.lastIndexOf("."));
        // 前端往后端传数据编码集
        request.setCharacterEncoding("utf-8");
        // 后端往服务器端输出字符流编码集
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if ("/findAll".equals(path)){
            System.out.println("..........");
            // 查询所有
            List<User> users = dao.selectAll();
            // 前台输出数据
            // 转发：业务逻辑处理未完成,交给更便于编写样式、动效的jsp页面来显示数据
            // 1.绑定数据
            request.setAttribute("users",users);
            /*// 2.获取转发器
            RequestDispatcher rd = request.getRequestDispatcher("list_user.jsp");
            // 3.转发
            rd.forward(request,response);*/

            // 2.3.合成一步
            request.getRequestDispatcher("list_user.jsp").forward(request,response);



            /*out.print("<table align='center' width='80%' cellspacing='0' cellpadding='0'>");
            out.print("<tr><th>ID</th><th>用户名</th><th>密码</th><th>电话</th><th>地址</th><th>注册时间</th><th>修改时间</th><th>状态</th></tr>");
            for (User user:users){
                out.print("<tr>" +
                        "<td>"+user.getId()+"</td>" +
                        "<td>"+user.getName()+"</td>" +
                        "<td>"+user.getPassword()+"</td>" +
                        "<td>"+user.getPhone()+"</td>" +
                        "<td>"+user.getAddress()+"</td>" +
                        "<td>"+user.getCreateTime()+"</td>" +
                        "<td>"+user.getUpdateTime()+"</td>" +
                        "<td>"+user.getStatus()+"</td>" +
                        "</tr>");
            }
            out.print("</table>");*/
        }else if ("/insert".equals(path)){
            // 从前端获取表单提交过来的数据
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            String phone = request.getParameter("phone");
            String address = request.getParameter("address");
            // 添加新用户
            dao.insert(new User(name,password,phone,address));

            // 重定向：页面跳转到列表页
            response.sendRedirect("findAll.user");
        }else if ("/delete".equals(path)){
            int id = Integer.parseInt(request.getParameter("id"));
            // 删除
            dao.delete(id);
            // 重定向：页面跳转到列表页
            response.sendRedirect("findAll.user");
        }else if ("/load".equals(path)){
            // 根据id查询
            int id = Integer.parseInt(request.getParameter("id"));

            User user = dao.selectById(id);
            request.setAttribute("user",user);
            request.getRequestDispatcher("update_user.jsp").forward(request,response);

        }else if ("/update".equals(path)){
            // 更新
            int id = Integer.parseInt(request.getParameter("id"));
            User user = dao.selectById(id);
            // user对象的属性设置
            String name = request.getParameter("name");
            user.setName(name);
            String password = request.getParameter("password");
            user.setPassword(password);
            String phone = request.getParameter("phone");
            user.setPhone(phone);
            String address = request.getParameter("address");
            user.setAddress(address);

            dao.update(user);

            response.sendRedirect("findAll.user");
        }else {
            // 重定向：业务逻辑处理完成后，页面跳转的过程
            // 只要没匹配到固定的若干url，都会重定向到404.jsp错误页面
            response.sendRedirect("404.jsp");
        }
    }

    /*public void findAll(HttpServletResponse response) throws IOException{
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        UserDAO dao = new UserDAOImpl();
        List<User> users = dao.selectAll();
        for (User user : users){
            out.print("<p>" + user + "</p>");
        }
    }
    public void insert(){
        UserDAO dao = new UserDAOImpl();
        dao.insert(new User("","","",""));
    }*/
}
